package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Slf4j
@Configuration
public class MvcConfig implements WebMvcConfigurer {


    @Autowired
    private RequestInterceptor requestInterceptor;

    /**
     * 支持跨域
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedOrigins("*")
                .allowedMethods("*");
    }


    // 临时放开 回话限制
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加需要鉴权的接口
        registry.addInterceptor(requestInterceptor).addPathPatterns("/**")
                // 放行的接口
                .excludePathPatterns(
                        "/user/login"
                )
                //设置拦截器的执行顺序为-1，表示优先级最高。
                .order(-1);
    }
}

